from django.urls import reverse
from django.contrib import admin
from orders.models import Order, OrderItem
# Register your models here.


class OrderItemInline(admin.TabularInline):
    model = OrderItem
    raw_id_fields = ['product']

def order_pdf(obj):
    return '<a href="{}">PDF</a>'.format(reverse('orders:admin_order_pdf', args=[obj.id]))
order_pdf.allow_tags = True
order_pdf.short_description = 'PDF bill'


class OrderAdmin(admin.ModelAdmin):
    list_display = ['id', 'first_name', 'last_name', 'email','address', 'postal_code', 'city', 'paid', 'created', 'update', order_pdf]
    list_filter = ['paid', 'created', 'update']
    inlines = [OrderItemInline]

admin.site.register(Order, OrderAdmin)
"""
我们在 OrderItem 使用 ModelInline 来把它引用为 OrderAdmin 类的内联元素。一个内联元素允许你在同一编辑页引用模型，并且将这个模型作为父模
"""